// pages/operation/index.js
const app = getApp();
const domain = app.globalData.domain

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    filterList: [],
    sortList: [],
    posName: '全部',
    posList: [],
    pickerVis: false,
    hiddenIds: [],
    searchText: null
  },
  toggleItem(e) {
    const id = e.currentTarget.dataset.id
    console.log(id)
    if (this.data.hiddenIds?.includes(id)) {
      const nList = this.data.hiddenIds.filter(_ => _ != id)
      this.setData({
        hiddenIds: nList
      })
    } else {
      this.setData({
        hiddenIds: [...this.data.hiddenIds, id]
      })
    }
    setTimeout(() => {
      console.log(this.data.hiddenIds)
    })
  },
  onBack() {
    console.log(1)
    wx.navigateBack({
      delta: 1
    })
  },
  onPicker1Confirm(e) {
    console.log(e)
    const {
      value
    } = e.detail
    this.setData({
      posName: value?.[0]
    })
  },
  choosePos() {
    this.setData({
      pickerVis: true
    })
  },
  search(e) {
    const {
      value
    } = e.detail
    const filterList = this.data.list?.filter(item => {
      return item?.name?.indexOf(value) !== -1
    })

    const sortList = this.getSortList(filterList)
    console.log('sortList', sortList)
    this.setData({
      filterList,
      sortList,
      searchText: value
    })
  },
  getPosList(list) {
    const nameList = []
    const nList = []
    list?.forEach(item => {
      if (item.posName && !nameList.includes(item.posName)) {
        nameList.push(item.posName)
        nList.push({
          label: item.posName,
          value: item.posName,
        })
      }
    })
    return [{
      label: '全部',
      value: '全部',
    }, ...nList]
  },
  getSortList(list) {
    let nList = []
    list?.forEach(item => {
      if (item.posName && item.childDeviceName) {
        const childDevice = `${item.childDeviceName} - ${item.childDeviceIdx}`
        const matchName = `${item.posName} - ${childDevice}`
        const fIndex = nList.findIndex(_ => _.posName === item.posName && _.childDevice === childDevice)
        // console.log('childDevice', childDevice, item.posName,fIndex)

        if (fIndex === -1) {
          nList.push({
            matchName,
            posName: item.posName,
            childDevice,
            children: [item]
          })
        } else {
          nList[fIndex].children.push(item)
        }
      }
    })
    return nList
  },
  init(options) {
    // wx.showLoading({
    //   title: '加载中',
    // })
    wx.request({
      url: `${domain}/admin-api/smc/scene/metric?sceneId=${options.id}`,
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: res => {
        wx.hideLoading()
        if (res?.data?.error === "Unauthorized") {
          wx.removeStorageSync('token')
          wx.reLaunch({
            url: '../login/login',
          })
          return;
        }
        console.log(res)
        const posList = this.getPosList(res?.data?.data?.list || [])
        const sortList = this.getSortList(res?.data?.data?.list || [])
        // const posName = res?.data?.data?.list?.[0]?.posName
        let filterList = res?.data?.data?.list
        console.log('sortList', sortList)

        this.setData({
          posList,
          sortList,
          list: res?.data?.data?.list,
          filterList,
          // posName,
        }, () => {
          if (this.data.searchText) {
            this.search({
              detail: {
                value: this.data.searchText
              }
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const interId = setInterval(() => {
      this.init(options)
    }, 5000);
    this.setData({
      options,
      interId
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.options.id) {
      this.init(this.data.options)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.data.interId)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})